package com.mezz.bones.im.common.result;

import com.mezz.bones.im.common.enums.ActionCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @Author: zhuang
 * @Date: 2024/12/12 14:56
 * @Description: TODO
 * @Version: 1.0
 **/

@Data
@AllArgsConstructor
public class ImResult<T> {

    /**
     * 响应服务器时间戳
     */
    private long timestamp;

    /**
     * 业务的key信息
     */
    private String traceId;

    /**
     * 响应具体的业务类型
     */
    private String actionCode;

    /**
     * 错误code编码
     */
    private String code;

    /**
     * 错误信息
     */
    private String errmsg;

    /**
     * 结果集数据
     */
    private T data;

    public boolean isOk(){
        return ImResultCode.SUCCESS.getCode().equals(this.code);
    }
    public ImResult(){
        this.timestamp = System.currentTimeMillis();
        this.traceId = "non";
        this.actionCode = ActionCodeEnum.SERVER_ERROR.getCode();
    }

    public static <T> ImResult<T> data(ImResultCode resultCode, String traceId, String actionCode, T data) {
        ImResult<T> result = new ImResult<>();
        result.setTraceId(traceId);
        result.setActionCode(actionCode);
        result.setCode(resultCode.getCode());
        result.setErrmsg(resultCode.getMessage());
        result.setData(data);
        return result;
    }

    public static <T> ImResult<T> ok(String traceId, String actionCode, T data) {
        return data(ImResultCode.SUCCESS, traceId, actionCode, data);
    }

    public static <T> ImResult<?> errorData(String traceId, String actionCode, String errmsg, T data) {
        return errorData(traceId, actionCode, ImResultCode.SYS_ERROR.getCode(), errmsg, data);
    }

    public static <T> ImResult<?> errorData(String traceId, String actionCode, String code, String errmsg, T data) {
        ImResult<Object> result = new ImResult<>();
        result.setTraceId(traceId);
        result.setActionCode(actionCode);
        result.setCode(code);
        result.setErrmsg(errmsg);
        result.setData(data);
        return result;
    }


    public static <T> ImResult<?> errorMsg(String errmsg) {
        ImResult<Object> result = new ImResult<>();
        result.setCode(ImResultCode.SYS_ERROR.getCode());
        result.setErrmsg(errmsg);
        return result;
    }

    public static <T> ImResult<?> errorMsg(String code, String errmsg) {
        ImResult<Object> result = new ImResult<>();
        result.setErrmsg(errmsg);
        result.setCode(code);
        return result;
    }

    public static <T> ImResult<?> errorMsg(String traceId, String actionCode, String errmsg) {
        ImResult<Object> result = new ImResult<>();
        result.setTraceId(traceId);
        result.setActionCode(actionCode);
        result.setCode(ImResultCode.SYS_ERROR.getCode());
        result.setErrmsg(errmsg);
        return result;
    }

    public ImResult<T> data(T data){
        this.data = data;
        return this;
    }

    public ImResult<T> actionCode(String actionCode){
        this.actionCode = actionCode;
        return this;
    }
}
